package com.jh.paymentchannel.helipay.service;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.jifu.DateTime;

import cn.jh.common.utils.UUIDGenerator;
import net.sf.json.JSONObject;
@Controller
@EnableAutoConfiguration
public class BangJSKService{
private static final Logger log = LoggerFactory.getLogger(BangJSKService.class);
	
		@Autowired
		Util util;
		private String merchant_no = "C1800001614";

		@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/pay/bkp")
		public @ResponseBody Object bangJSK(HttpServletRequest request, 
				@RequestParam(value = "userId") long userId,
				@RequestParam(value = "loginPhone") String loginPhone,
				@RequestParam(value = "cardNo") String cardNo
				) {
		Map map = new HashMap();
		RestTemplate restTemplate=new RestTemplate();
		URI uri = util.getServiceUrl("user", "error url request!");
		String url = uri.toString() + "/v1.0/user/bank/find/bankphone";
		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", String.valueOf(userId));
		requestEntity.add("cardNo", cardNo);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("接口/v1.0/user/bank/find/bankphone--RESULT================"+result);
		JSONObject jsonObject =  JSONObject.fromObject(result);
		JSONObject resultObj  =  jsonObject.getJSONObject("result");
		String userName =  resultObj.getString("userName");
		 String phone = resultObj.getString("phone"); //预留手机号码
		 String idcard = resultObj.getString("idcard");//身份证号
		 String bankName = resultObj.getString("bankName");
		/**
		 * 绑结算卡
		 */
		RestTemplate restTemplate2=new RestTemplate();
		URI uri2 = util.getServiceUrl("paymentchannel", "error url request!");
		String url2 = uri2.toString() + "/v1.0/paymentchannel/topup/settlementCardBind";
		requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("P1_bizType", "SettlementCardBind");
		requestEntity.add("P2_customerNumber", merchant_no);
		requestEntity.add("P3_userId", String.valueOf(userId));
		requestEntity.add("P4_orderId", UUIDGenerator.getUUID());
		requestEntity.add("P5_payerName", userName);
		requestEntity.add("P6_idCardType", "IDCARD");
		requestEntity.add("P7_idCardNo", idcard);
		requestEntity.add("P8_cardNo", cardNo);
		requestEntity.add("P9_phone", phone);
		requestEntity.add("P10_bankUnionCode", "");//银行行号
		log.info("===================requestEntity=================="+requestEntity.toString());
		result = restTemplate2.postForObject(url2, requestEntity, String.class);
		log.info("===================result=================="+result.toString());
		JSONObject objs = JSONObject.fromObject(result);
		if("绑结算卡成功".equals(objs.getString("message"))) {
			JSONObject jobj = objs.getJSONObject("json");
			if(jobj.containsKey("rt7_bindStatus")&&"FAIL".equals(jobj.getString("rt7_bindStatus"))) {
				map.put("resp_code", "999999");
				map.put("resp_message", "绑卡失败");
				return map; 
			}
			String bindId = jobj.getString("rt10_bindId");
			 url2 = uri2.toString() + "/v1.0/paymentchannel/hlregister/add";
			requestEntity  = new LinkedMultiValueMap<String, String>();
			requestEntity.add("userId", String.valueOf(userId));
			requestEntity.add("exemption", "0");
			requestEntity.add("cardNo", idcard);
			requestEntity.add("phone", phone);
			requestEntity.add("name", userName);
			requestEntity.add("bindId",bindId);
			requestEntity.add("bankName", bankName);
			requestEntity.add("bankCard", cardNo);
			requestEntity.add("loginPhone", loginPhone);
			requestEntity.add("cardType", "2");
			requestEntity.add("status", "0");
			requestEntity.add("balance", "0");
			requestEntity.add("frozenBalance", "0");
			log.info("===================requestEntity=================="+requestEntity.toString());
			result = restTemplate2.postForObject(url2, requestEntity, String.class);
			log.info("===================result=================="+result.toString());
			JSONObject obj = JSONObject.fromObject(result);
			JSONObject rlt = obj.getJSONObject("result");
			map.put("resp_code", "000000");
			map.put("result", rlt.getString("bindId"));
			map.put("resp_message", "绑卡成功");
		}else {
				map.put("resp_code", "999999");
				map.put("resp_message", objs.getString("message"));
		}
		return map; 
	}
}
